<!DOCTYPE html>
<!-- saved from url=(0056)https://www.runoob.com/python/python-multithreading.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	
  <meta property="qc:admins" content="465267610762567726375">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Python 多线程 | 菜鸟教程</title>

  <link rel="dns-prefetch" href="https://s.w.org/">
<link rel="canonical" href="http://www.runoob.com/python/python-multithreading.html">
<meta name="keywords" content="Python 多线程">
<meta name="description" content="Python 多线程 多线程类似于同时执行多个不同程序，多线程运行有如下优点：  使用线程可以把占据长时间的程序中的任务放到后台去处理。 用户界面可以更加吸引人，这样比如用户点击了一个按钮去触发某些事件的处理，可以弹出一个进度条来显示处理的进度 程序的运行速度可能加快 在一些等待的任务实现上如用户输入、文件读写和网络收发数据等，线程就比较有用了。在这种情况下我们可以释放一些珍贵的资源如内存占用等等。  线程在执行过程中与进程还是有区别的..">
		
	<link rel="shortcut icon" href="https://static.runoob.com/images/favicon.ico" mce_href="//static.runoob.com/images/favicon.ico" type="image/x-icon">
	<link rel="stylesheet" href="./Python 多线程 _ 菜鸟教程_files/style.css" type="text/css" media="all">	
	<link rel="stylesheet" href="./Python 多线程 _ 菜鸟教程_files/font-awesome.min.css" media="all">	
  <!--[if gte IE 9]><!-->
  <script src="./Python 多线程 _ 菜鸟教程_files/osd.js"></script><script src="./Python 多线程 _ 菜鸟教程_files/f(4).txt"></script><script type="text/javascript" async="" src="./Python 多线程 _ 菜鸟教程_files/analytics.js"></script><script src="./Python 多线程 _ 菜鸟教程_files/hm.js"></script><script src="./Python 多线程 _ 菜鸟教程_files/f(5).txt" id="google_shimpl"></script><script src="./Python 多线程 _ 菜鸟教程_files/jquery.min.js"></script>
  <!--<![endif]-->
  <!--[if lt IE 9]>
     <script src="https://cdn.staticfile.org/jquery/1.9.1/jquery.min.js"></script>
     <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="https://static.runoob.com/images/icon/mobile-icon.png">
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
<meta http-equiv="origin-trial" content="A2shzsdPO+RKe83bUqT9oVkYwGZN6j9O7nrcOASNFPuQz8HefgVYb9qAqn6coNCSDIRtXoi6ybCrjEsYh3caFgIAAAB7eyJvcmlnaW4iOiJodHRwczovL2RvdWJsZWNsaWNrLm5ldDo0NDMiLCJmZWF0dXJlIjoiVHJ1c3RUb2tlbnMiLCJleHBpcnkiOjE2MTM0OTU4NjgsImlzU3ViZG9tYWluIjp0cnVlLCJpc1RoaXJkUGFydHkiOnRydWV9"><link rel="stylesheet" type="text/css" href="./Python 多线程 _ 菜鸟教程_files/prettify.css"><link rel="preload" href="./Python 多线程 _ 菜鸟教程_files/f(6).txt" as="script"><script type="text/javascript" src="./Python 多线程 _ 菜鸟教程_files/f(6).txt"></script></head>
<body style="">

<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="https://www.runoob.com/python/index.php">
      <input class="placeholder" placeholder="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="https://www.runoob.com/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="https://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" placeholder="搜索……" autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="https://www.runoob.com/">首页</a></li>
				<li><a href="https://www.runoob.com/html/html-tutorial.html">HTML</a></li>
				<li><a href="https://www.runoob.com/css/css-tutorial.html">CSS</a></li>
				<li><a href="https://www.runoob.com/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="https://www.runoob.com/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="https://www.runoob.com/vue2/vue-tutorial.html">Vue</a></li>
				<li><a href="https://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="https://www.runoob.com/python3/python3-tutorial.html">Python3</a></li>
				<li><a href="https://www.runoob.com/python/python-tutorial.html">Python2</a></li>
				<li><a href="https://www.runoob.com/java/java-tutorial.html">Java</a></li>
				<li><a href="https://www.runoob.com/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="https://www.runoob.com/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="https://www.runoob.com/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="https://www.runoob.com/go/go-tutorial.html">Go</a></li>
				<li><a href="https://www.runoob.com/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="https://www.runoob.com/browser-history">本地书签</a></li>
				<!--
			
				<li><a href="/w3cnote/knowledge-start.html" style="font-weight: bold;" onclick="_hmt.push(['_trackEvent', '星球', 'click', 'start'])" title="我的圈子">我的圈子</a></li>				
				<li><a href="javascript:;" class="runoob-pop">登录</a></li>
				-->
      		</ul>
			<ul class="mobile-nav">
				<li><a href="https://www.runoob.com/">首页</a></li>
				<li><a href="https://www.runoob.com/html/html-tutorial.html">HTML</a></li>
				<li><a href="https://www.runoob.com/css/css-tutorial.html">CSS</a></li>
				<li><a href="https://www.runoob.com/js/js-tutorial.html">JS</a></li>
				<li><a href="https://www.runoob.com/browser-history">本地书签</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">Python 基础教程	<a data-cate="48" href="javascript:void(0);" title="夜间模式" id="moon" style="display: none;"><i class="fa fa-moon-o" aria-hidden="true" style="font-size: 1.4em;float: right;margin: 4px 6px 0;"></i></a>
	<a data-cate="48" style="" href="javascript:void(0);" title="日间模式" id="sun"><i class="fa fa-sun-o" aria-hidden="true" style="font-size: 1.4em;float: right;margin: 4px 6px 0;"></i></a>
	</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="Python 基础教程" href="https://www.runoob.com/python/python-tutorial.html">
			Python 基础教程			</a>
						<a target="_top" title="Python 简介" href="https://www.runoob.com/python/python-intro.html">
			Python 简介			</a>
						<a target="_top" title="Python 环境搭建" href="https://www.runoob.com/python/python-install.html">
			Python 环境搭建			</a>
			<a target="_top" title="Python 中文编码" href="https://www.runoob.com/python/python-chinese-encoding.html"> Python 中文编码 </a>			<a target="_top" title="Python 基础语法" href="https://www.runoob.com/python/python-basic-syntax.html">
			Python 基础语法			</a>
						<a target="_top" title="Python 变量类型" href="https://www.runoob.com/python/python-variable-types.html">
			Python 变量类型			</a>
						<a target="_top" title="Python 运算符" href="https://www.runoob.com/python/python-operators.html">
			Python 运算符			</a>
						<a target="_top" title="Python 条件语句" href="https://www.runoob.com/python/python-if-statement.html">
			Python 条件语句			</a>
						<a target="_top" title="Python 循环语句" href="https://www.runoob.com/python/python-loops.html">
			Python 循环语句			</a>
						<a target="_top" title="Python While 循环语句" href="https://www.runoob.com/python/python-while-loop.html">
			Python While 循环语句			</a>
						<a target="_top" title="Python for 循环语句" href="https://www.runoob.com/python/python-for-loop.html">
			Python for 循环语句			</a>
						<a target="_top" title="Python 循环嵌套" href="https://www.runoob.com/python/python-nested-loops.html">
			Python 循环嵌套			</a>
						<a target="_top" title="Python break 语句" href="https://www.runoob.com/python/python-break-statement.html">
			Python break 语句			</a>
						<a target="_top" title="Python continue  语句" href="https://www.runoob.com/python/python-continue-statement.html">
			Python continue  语句			</a>
						<a target="_top" title="Python pass 语句" href="https://www.runoob.com/python/python-pass-statement.html">
			Python pass 语句			</a>
						<a target="_top" title="Python Number(数字)" href="https://www.runoob.com/python/python-numbers.html">
			Python Number(数字)			</a>
						<a target="_top" title="Python 字符串" href="https://www.runoob.com/python/python-strings.html">
			Python 字符串			</a>
						<a target="_top" title="Python 列表(List)" href="https://www.runoob.com/python/python-lists.html">
			Python 列表(List)			</a>
						<a target="_top" title="Python 元组" href="https://www.runoob.com/python/python-tuples.html">
			Python 元组			</a>
						<a target="_top" title="Python 字典(Dictionary)" href="https://www.runoob.com/python/python-dictionary.html">
			Python 字典(Dictionary)			</a>
						<a target="_top" title="Python 日期和时间" href="https://www.runoob.com/python/python-date-time.html">
			Python 日期和时间			</a>
						<a target="_top" title="Python 函数" href="https://www.runoob.com/python/python-functions.html">
			Python 函数			</a>
						<a target="_top" title="Python 模块" href="https://www.runoob.com/python/python-modules.html">
			Python 模块			</a>
						<a target="_top" title="Python 文件I/O" href="https://www.runoob.com/python/python-files-io.html">
			Python 文件I/O			</a>
			<a target="_top" title="Python File 方法" href="https://www.runoob.com/python/file-methods.html">Python File 方法</a>			<a target="_top" title="Python 异常处理" href="https://www.runoob.com/python/python-exceptions.html">
			Python 异常处理			</a>
			<a target="_top" title="Python OS 文件/目录方法" href="https://www.runoob.com/python/os-file-methods.html">Python OS 文件/目录方法</a>
<a target="_top" title="Python 内置函数" href="https://www.runoob.com/python/python-built-in-functions.html">Python 内置函数</a>
<br><h2 class="left"><span class="left_h2">Python</span> 高级教程</h2>			<a target="_top" title="Python 面向对象" href="https://www.runoob.com/python/python-object.html">
			Python 面向对象			</a>
						<a target="_top" title="Python 正则表达式" href="https://www.runoob.com/python/python-reg-expressions.html">
			Python 正则表达式			</a>
						<a target="_top" title="Python CGI 编程" href="https://www.runoob.com/python/python-cgi.html">
			Python CGI 编程			</a>
						<a target="_top" title="Python 操作 MySQL 数据库" href="https://www.runoob.com/python/python-mysql.html">
			Python MySQL			</a>
			<a target="_top" title="Python 网络编程" href="https://www.runoob.com/python/python-socket.html"> Python 网络编程 </a>			<a target="_top" title="Python SMTP发送邮件" href="https://www.runoob.com/python/python-email.html">
			Python SMTP			</a>
						<a target="_top" title="Python 多线程" href="https://www.runoob.com/python/python-multithreading.html" style="background-color: rgb(150, 185, 125); font-weight: bold; color: rgb(255, 255, 255);">
			Python 多线程			</a>
						<a target="_top" title="Python XML 解析" href="https://www.runoob.com/python/python-xml.html">
			Python XML 解析			</a>
						<a target="_top" title="Python GUI 编程(Tkinter)" href="https://www.runoob.com/python/python-gui-tkinter.html">
			Python GUI 编程(Tkinter)			</a>
						<a target="_top" title="Python2.x 与 3​​.x 版本区别" href="https://www.runoob.com/python/python-2x-3x.html">
			Python2.x 与 3​​.x 版本区别			</a>
						<a target="_top" title="Python IDE" href="https://www.runoob.com/python/python-ide.html">
			Python IDE			</a>
						<a target="_top" title="Python JSON" href="https://www.runoob.com/python/python-json.html">
			Python JSON			</a>
						<a target="_top" title="Python 100例" href="https://www.runoob.com/python/python-100-examples.html">
			Python 100例			</a>
			<a target="_blank" title="Python 测验" href="https://www.runoob.com/quiz/python-quiz.html"> Python 测验 </a>	
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article" style="background-color: rgb(238, 238, 238);">
			<div class="article-heading-ad" style="display: none;">
		
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link"><a href="https://www.runoob.com/python/python-email.html"><i style="font-size:16px;" class="fa fa-arrow-left" aria-hidden="true"></i></a> <a href="https://www.runoob.com/python/python-email.html" rel="prev" title="Python SMTP发送邮件">Python SMTP发送邮件</a> </div>
			<div class="next-design-link"><a href="https://www.runoob.com/python/python-xml.html" rel="next" title="Python XML 解析">Python XML 解析</a> <a href="https://www.runoob.com/python/python-xml.html"><i style="font-size:16px;" class="fa fa-arrow-right" aria-hidden="true"></i></a></div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content" deep="5">
			
			<h1>Python 多线程</h1>
<p>多线程类似于同时执行多个不同程序，多线程运行有如下优点：</p>
<ul>
<li>使用线程可以把占据长时间的程序中的任务放到后台去处理。</li>
<li>用户界面可以更加吸引人，这样比如用户点击了一个按钮去触发某些事件的处理，可以弹出一个进度条来显示处理的进度</li>
<li>程序的运行速度可能加快</li>
<li>在一些等待的任务实现上如用户输入、文件读写和网络收发数据等，线程就比较有用了。在这种情况下我们可以释放一些珍贵的资源如内存占用等等。</li>
</ul>
<p>线程在执行过程中与进程还是有区别的。每个独立的进程有一个程序运行的入口、顺序执行序列和程序的出口。但是线程不能够独立执行，必须依存在应用程序中，由应用程序提供多个线程执行控制。
</p>
<p>每个线程都有他自己的一组CPU寄存器，称为线程的上下文，该上下文反映了线程上次运行该线程的CPU寄存器的状态。
</p><p>指令指针和堆栈指针寄存器是线程上下文中两个最重要的寄存器，线程总是在进程得到上下文中运行的，这些地址都用于标志拥有线程的进程地址空间中的内存。
</p>
<ul>

<li>线程可以被抢占（中断）。</li>
<li>在其他线程正在运行时，线程可以暂时搁置（也称为睡眠） -- 这就是线程的退让。
</li>
</ul>
<br data-filtered="filtered">
<h2>开始学习Python线程</h2>
<p>Python中使用线程有两种方式：函数或者用类来包装线程对象。</p>
<p>函数式：调用thread模块中的start_new_thread()函数来产生新线程。语法如下:</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">thread</span><span class="pun">.</span><span class="pln">start_new_thread </span><span class="pun">(</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">,</span><span class="pln"> args</span><span class="pun">[,</span><span class="pln"> kwargs</span><span class="pun">]</span><span class="pln"> </span><span class="pun">)</span></pre>
<p>参数说明:</p>
<ul>
<li>function - 线程函数。</li>
<li>args - 传递给线程函数的参数,他必须是个tuple类型。</li>
<li>kwargs - 可选参数。</li>
</ul>
<div class="example"> 
<h2 class="example">实例(Python 2.0+)</h2> 
<div class="example_code" style="background-color: rgb(238, 238, 238);">
<div class="hl-main" style="background-color: rgb(238, 238, 238);"><span class="hl-comment">#!/usr/bin/python</span><span class="hl-code">
</span><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">
 
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">thread</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">time</span><span class="hl-code">
 
</span><span class="hl-comment"># 为线程定义一个函数</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">print_time</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">threadName</span><span class="hl-code">, </span><span class="hl-identifier">delay</span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">count</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">
   </span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-identifier">count</span><span class="hl-code"> &lt; </span><span class="hl-number">5</span><span class="hl-code">:
      </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">sleep</span><span class="hl-brackets">(</span><span class="hl-identifier">delay</span><span class="hl-brackets">)</span><span class="hl-code">
      </span><span class="hl-identifier">count</span><span class="hl-code"> += </span><span class="hl-number">1</span><span class="hl-code">
      </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">%s: %s</span><span class="hl-quotes">"</span><span class="hl-code"> % </span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">threadName</span><span class="hl-code">, </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">ctime</span><span class="hl-brackets">(</span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">time</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-comment"># 创建两个线程</span><span class="hl-code">
</span><span class="hl-reserved">try</span><span class="hl-code">:
   </span><span class="hl-identifier">thread</span><span class="hl-code">.</span><span class="hl-identifier">start_new_thread</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">print_time</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Thread-1</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">thread</span><span class="hl-code">.</span><span class="hl-identifier">start_new_thread</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">print_time</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Thread-2</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">4</span><span class="hl-code">, </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">except</span><span class="hl-code">:
   </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Error: unable to start thread</span><span class="hl-quotes">"</span><span class="hl-code">
 
</span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-number">1</span><span class="hl-code">:
   </span><span class="hl-reserved">pass</span></div>
</div>
</div>
<p>执行以上程序输出结果如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">17</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">19</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">19</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">21</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">23</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">23</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">27</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">31</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">15</span><span class="pun">:</span><span class="lit">42</span><span class="pun">:</span><span class="lit">35</span><span class="pln"> </span><span class="lit">2009</span></pre>
<p>线程的结束一般依靠线程函数的自然结束；也可以在线程函数中调用thread.exit()，他抛出SystemExit exception，达到退出线程的目的。
</p>
<hr>
<h2>线程模块</h2>
<p>Python通过两个标准库thread和threading提供对线程的支持。thread提供了低级别的、原始的线程以及一个简单的锁。</p>
<p>threading 模块提供的其他方法： </p>
<ul>
<li>threading.currentThread(): 返回当前的线程变量。 </li>
<li>threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前，不包括启动前和终止后的线程。 </li>
<li>threading.activeCount(): 返回正在运行的线程数量，与len(threading.enumerate())有相同的结果。</li>
</ul>
<p>除了使用方法外，线程模块同样提供了Thread类来处理线程，Thread类提供了以下方法:</p>
<ul>
<li><b>run():</b> 用以表示线程活动的方法。</li>
<li><b>start():</b>启动线程活动。<p></p></li>
<li><b> join([time]):</b> 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。</li>
<li><b> isAlive():</b> 返回线程是否活动的。</li>
<li><b> getName():</b> 返回线程名。</li>
<li><b> setName():</b> 设置线程名。</li>
</ul>
<hr>
<h2>使用Threading模块创建线程</h2>
<p>使用Threading模块创建线程，直接从threading.Thread继承，然后重写__init__方法和run方法：</p>
<div class="example"> 
<h2 class="example">实例(Python 2.0+)</h2> 
<div class="example_code" style="background-color: rgb(238, 238, 238);">
<div class="hl-main" style="background-color: rgb(238, 238, 238);"><span class="hl-comment">#!/usr/bin/python</span><span class="hl-code">
</span><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">
 
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">threading</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">time</span><span class="hl-code">
 
</span><span class="hl-identifier">exitFlag</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">
 
</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">myThread</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">threading</span><span class="hl-code">.</span><span class="hl-identifier">Thread</span><span class="hl-brackets">)</span><span class="hl-code">:   </span><span class="hl-comment">#继承父类threading.Thread</span><span class="hl-code">
    </span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">threadID</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">counter</span><span class="hl-brackets">)</span><span class="hl-code">:
        </span><span class="hl-identifier">threading</span><span class="hl-code">.</span><span class="hl-identifier">Thread</span><span class="hl-code">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">threadID</span><span class="hl-code"> = </span><span class="hl-identifier">threadID</span><span class="hl-code">
        </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-identifier">name</span><span class="hl-code">
        </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">counter</span><span class="hl-code"> = </span><span class="hl-identifier">counter</span><span class="hl-code">
    </span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">run</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-code">:                   </span><span class="hl-comment">#把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 </span><span class="hl-code">
        </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Starting </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">
        </span><span class="hl-identifier">print_time</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">counter</span><span class="hl-code">, </span><span class="hl-number">5</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Exiting </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">
 
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">print_time</span><span class="hl-brackets">(</span><span class="hl-identifier">threadName</span><span class="hl-code">, </span><span class="hl-identifier">delay</span><span class="hl-code">, </span><span class="hl-identifier">counter</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-identifier">counter</span><span class="hl-code">:
        </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">exitFlag</span><span class="hl-code">:
            </span><span class="hl-brackets">(</span><span class="hl-identifier">threading</span><span class="hl-code">.</span><span class="hl-identifier">Thread</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">exit</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">sleep</span><span class="hl-brackets">(</span><span class="hl-identifier">delay</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">%s: %s</span><span class="hl-quotes">"</span><span class="hl-code"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">threadName</span><span class="hl-code">, </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">ctime</span><span class="hl-brackets">(</span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">time</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">counter</span><span class="hl-code"> -= </span><span class="hl-number">1</span><span class="hl-code">
 
</span><span class="hl-comment"># 创建新线程</span><span class="hl-code">
</span><span class="hl-identifier">thread1</span><span class="hl-code"> = </span><span class="hl-identifier">myThread</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Thread-1</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">thread2</span><span class="hl-code"> = </span><span class="hl-identifier">myThread</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Thread-2</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-comment"># 开启线程</span><span class="hl-code">
</span><span class="hl-identifier">thread1</span><span class="hl-code">.</span><span class="hl-identifier">start</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">thread2</span><span class="hl-code">.</span><span class="hl-identifier">start</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Exiting Main Thread</span><span class="hl-quotes">"</span></div>
</div>
</div>
<p>以上程序执行结果如下；</p>
<pre class="prettyprint prettyprinted" style=""><span class="typ">Starting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln">
</span><span class="typ">Starting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln">
</span><span class="typ">Exiting</span><span class="pln"> </span><span class="typ">Main</span><span class="pln"> </span><span class="typ">Thread</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">03</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">04</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">04</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">05</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">06</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">06</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">07</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Exiting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">08</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">10</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Mar</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">12</span><span class="pln"> </span><span class="lit">2013</span><span class="pln">
</span><span class="typ">Exiting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span></pre>
<hr>
<h2>线程同步</h2>
<p>如果多个线程共同对某个数据修改，则可能出现不可预料的结果，为了保证数据的正确性，需要对多个线程进行同步。
</p>
<p>
使用Thread对象的Lock和Rlock可以实现简单的线程同步，这两个对象都有acquire方法和release方法，对于那些需要每次只允许一个线程操作的数据，可以将其操作放到acquire和release方法之间。如下：
</p>
<p>多线程的优势在于可以同时运行多个任务（至少感觉起来是这样）。但是当线程需要共享数据时，可能存在数据不同步的问题。</p>
<p>
考虑这样一种情况：一个列表里所有元素都是0，线程"set"从后向前把所有元素改成1，而线程"print"负责从前往后读取列表并打印。</p>
<p>
那么，可能线程"set"开始改的时候，线程"print"便来打印列表了，输出就成了一半0一半1，这就是数据的不同步。为了避免这种情况，引入了锁的概念。
</p>
<p>
锁有两种状态——锁定和未锁定。每当一个线程比如"set"要访问共享数据时，必须先获得锁定；如果已经有别的线程比如"print"获得锁定了，那么就让线程"set"暂停，也就是同步阻塞；等到线程"print"访问完毕，释放锁以后，再让线程"set"继续。
</p>
<p>
经过这样的处理，打印列表时要么全部输出0，要么全部输出1，不会再出现一半0一半1的尴尬场面。</p>

<div class="example"> 
<h2 class="example">实例(Python 2.0+)</h2> 
<div class="example_code" style="background-color: rgb(238, 238, 238);">
<div class="hl-main" style="background-color: rgb(238, 238, 238);"><span class="hl-comment">#!/usr/bin/python</span><span class="hl-code">
</span><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">
 
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">threading</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">time</span><span class="hl-code">
 
</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">myThread</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">threading</span><span class="hl-code">.</span><span class="hl-identifier">Thread</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">threadID</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">counter</span><span class="hl-brackets">)</span><span class="hl-code">:
        </span><span class="hl-identifier">threading</span><span class="hl-code">.</span><span class="hl-identifier">Thread</span><span class="hl-code">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">threadID</span><span class="hl-code"> = </span><span class="hl-identifier">threadID</span><span class="hl-code">
        </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-identifier">name</span><span class="hl-code">
        </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">counter</span><span class="hl-code"> = </span><span class="hl-identifier">counter</span><span class="hl-code">
    </span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">run</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-code">:
        </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Starting </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">
       </span><span class="hl-comment"># 获得锁，成功获得锁定后返回True</span><span class="hl-code">
       </span><span class="hl-comment"># 可选的timeout参数不填时将一直阻塞直到获得锁定</span><span class="hl-code">
       </span><span class="hl-comment"># 否则超时后将返回False</span><span class="hl-code">
        </span><span class="hl-identifier">threadLock</span><span class="hl-code">.</span><span class="hl-identifier">acquire</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">print_time</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">counter</span><span class="hl-code">, </span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-comment"># 释放锁</span><span class="hl-code">
        </span><span class="hl-identifier">threadLock</span><span class="hl-code">.</span><span class="hl-identifier">release</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">print_time</span><span class="hl-brackets">(</span><span class="hl-identifier">threadName</span><span class="hl-code">, </span><span class="hl-identifier">delay</span><span class="hl-code">, </span><span class="hl-identifier">counter</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-identifier">counter</span><span class="hl-code">:
        </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">sleep</span><span class="hl-brackets">(</span><span class="hl-identifier">delay</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">%s: %s</span><span class="hl-quotes">"</span><span class="hl-code"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">threadName</span><span class="hl-code">, </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">ctime</span><span class="hl-brackets">(</span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">time</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">counter</span><span class="hl-code"> -= </span><span class="hl-number">1</span><span class="hl-code">
 
</span><span class="hl-identifier">threadLock</span><span class="hl-code"> = </span><span class="hl-identifier">threading</span><span class="hl-code">.</span><span class="hl-identifier">Lock</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">threads</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code">
 
</span><span class="hl-comment"># 创建新线程</span><span class="hl-code">
</span><span class="hl-identifier">thread1</span><span class="hl-code"> = </span><span class="hl-identifier">myThread</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Thread-1</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">thread2</span><span class="hl-code"> = </span><span class="hl-identifier">myThread</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Thread-2</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-comment"># 开启新线程</span><span class="hl-code">
</span><span class="hl-identifier">thread1</span><span class="hl-code">.</span><span class="hl-identifier">start</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">thread2</span><span class="hl-code">.</span><span class="hl-identifier">start</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-comment"># 添加线程到线程列表</span><span class="hl-code">
</span><span class="hl-identifier">threads</span><span class="hl-code">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">thread1</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">threads</span><span class="hl-code">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">thread2</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-comment"># 等待所有线程完成</span><span class="hl-code">
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">t</span><span class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span class="hl-identifier">threads</span><span class="hl-code">:
    </span><span class="hl-identifier">t</span><span class="hl-code">.</span><span class="hl-identifier">join</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Exiting Main Thread</span><span class="hl-quotes">"</span></div>
</div>
</div>
<hr>
<h2>线程优先级队列（ Queue）
</h2>
<p> Python的Queue模块中提供了同步的、线程安全的队列类，包括FIFO（先入先出)队列Queue，LIFO（后入先出）队列LifoQueue，和优先级队列PriorityQueue。这些队列都实现了锁原语，能够在多线程中直接使用。可以使用队列来实现线程间的同步。</p>

<p>Queue模块中的常用方法:</p><p>
</p><ul>
<li>Queue.qsize() 返回队列的大小 </li>
<li>Queue.empty() 如果队列为空，返回True,反之False </li>
<li>Queue.full() 如果队列满了，返回True,反之False</li>
<li>Queue.full 与 maxsize 大小对应 </li>
<li>Queue.get([block[, timeout]])获取队列，timeout等待时间 </li>
<li>Queue.get_nowait() 相当Queue.get(False)</li>
<li>Queue.put(item) 写入队列，timeout等待时间 </li>
<li>Queue.put_nowait(item) 相当Queue.put(item, False)</li>
<li>Queue.task_done() 在完成一项工作之后，Queue.task_done()函数向任务已经完成的队列发送一个信号</li>
<li>Queue.join() 实际上意味着等到队列为空，再执行别的操作</li>
</ul>
<div class="example"> 
<h2 class="example">实例(Python 2.0+)</h2> 
<div class="example_code" style="background-color: rgb(238, 238, 238);">
<div class="hl-main" style="background-color: rgb(238, 238, 238);"><span class="hl-comment">#!/usr/bin/python</span><span class="hl-code">
</span><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">
 
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">Queue</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">threading</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">time</span><span class="hl-code">
 
</span><span class="hl-identifier">exitFlag</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">
 
</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">myThread</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">threading</span><span class="hl-code">.</span><span class="hl-identifier">Thread</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">threadID</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">q</span><span class="hl-brackets">)</span><span class="hl-code">:
        </span><span class="hl-identifier">threading</span><span class="hl-code">.</span><span class="hl-identifier">Thread</span><span class="hl-code">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">threadID</span><span class="hl-code"> = </span><span class="hl-identifier">threadID</span><span class="hl-code">
        </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-identifier">name</span><span class="hl-code">
        </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">q</span><span class="hl-code"> = </span><span class="hl-identifier">q</span><span class="hl-code">
    </span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">run</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-code">:
        </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Starting </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">
        </span><span class="hl-identifier">process_data</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">q</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Exiting </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">
 
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">process_data</span><span class="hl-brackets">(</span><span class="hl-identifier">threadName</span><span class="hl-code">, </span><span class="hl-identifier">q</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-reserved">not</span><span class="hl-code"> </span><span class="hl-identifier">exitFlag</span><span class="hl-code">:
        </span><span class="hl-identifier">queueLock</span><span class="hl-code">.</span><span class="hl-identifier">acquire</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-reserved">not</span><span class="hl-code"> </span><span class="hl-identifier">workQueue</span><span class="hl-code">.</span><span class="hl-identifier">empty</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">:
            </span><span class="hl-identifier">data</span><span class="hl-code"> = </span><span class="hl-identifier">q</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
            </span><span class="hl-identifier">queueLock</span><span class="hl-code">.</span><span class="hl-identifier">release</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
            </span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">%s processing %s</span><span class="hl-quotes">"</span><span class="hl-code"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">threadName</span><span class="hl-code">, </span><span class="hl-identifier">data</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">else</span><span class="hl-code">:
            </span><span class="hl-identifier">queueLock</span><span class="hl-code">.</span><span class="hl-identifier">release</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">sleep</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-identifier">threadList</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">"</span><span class="hl-string">Thread-1</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Thread-2</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Thread-3</span><span class="hl-quotes">"</span><span class="hl-brackets">]</span><span class="hl-code">
</span><span class="hl-identifier">nameList</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">"</span><span class="hl-string">One</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Two</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Three</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Four</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Five</span><span class="hl-quotes">"</span><span class="hl-brackets">]</span><span class="hl-code">
</span><span class="hl-identifier">queueLock</span><span class="hl-code"> = </span><span class="hl-identifier">threading</span><span class="hl-code">.</span><span class="hl-identifier">Lock</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">workQueue</span><span class="hl-code"> = </span><span class="hl-identifier">Queue</span><span class="hl-code">.</span><span class="hl-identifier">Queue</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">threads</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code">
</span><span class="hl-identifier">threadID</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">
 
</span><span class="hl-comment"># 创建新线程</span><span class="hl-code">
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">tName</span><span class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span class="hl-identifier">threadList</span><span class="hl-code">:
    </span><span class="hl-identifier">thread</span><span class="hl-code"> = </span><span class="hl-identifier">myThread</span><span class="hl-brackets">(</span><span class="hl-identifier">threadID</span><span class="hl-code">, </span><span class="hl-identifier">tName</span><span class="hl-code">, </span><span class="hl-identifier">workQueue</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">thread</span><span class="hl-code">.</span><span class="hl-identifier">start</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">threads</span><span class="hl-code">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">thread</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">threadID</span><span class="hl-code"> += </span><span class="hl-number">1</span><span class="hl-code">
 
</span><span class="hl-comment"># 填充队列</span><span class="hl-code">
</span><span class="hl-identifier">queueLock</span><span class="hl-code">.</span><span class="hl-identifier">acquire</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">word</span><span class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span class="hl-identifier">nameList</span><span class="hl-code">:
    </span><span class="hl-identifier">workQueue</span><span class="hl-code">.</span><span class="hl-identifier">put</span><span class="hl-brackets">(</span><span class="hl-identifier">word</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">queueLock</span><span class="hl-code">.</span><span class="hl-identifier">release</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-comment"># 等待队列清空</span><span class="hl-code">
</span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-reserved">not</span><span class="hl-code"> </span><span class="hl-identifier">workQueue</span><span class="hl-code">.</span><span class="hl-identifier">empty</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-reserved">pass</span><span class="hl-code">
 
</span><span class="hl-comment"># 通知线程是时候退出</span><span class="hl-code">
</span><span class="hl-identifier">exitFlag</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">
 
</span><span class="hl-comment"># 等待所有线程完成</span><span class="hl-code">
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">t</span><span class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span class="hl-identifier">threads</span><span class="hl-code">:
    </span><span class="hl-identifier">t</span><span class="hl-code">.</span><span class="hl-identifier">join</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">print</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">Exiting Main Thread</span><span class="hl-quotes">"</span></div>
</div>
</div>
<p>以上程序执行结果：</p>
<pre class="prettyprint prettyprinted" style=""><span class="typ">Starting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln">
</span><span class="typ">Starting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln">
</span><span class="typ">Starting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">3</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> processing </span><span class="typ">One</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> processing </span><span class="typ">Two</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">3</span><span class="pln"> processing </span><span class="typ">Three</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> processing </span><span class="typ">Four</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> processing </span><span class="typ">Five</span><span class="pln">
</span><span class="typ">Exiting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">3</span><span class="pln">
</span><span class="typ">Exiting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln">
</span><span class="typ">Exiting</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln">
</span><span class="typ">Exiting</span><span class="pln"> </span><span class="typ">Main</span><span class="pln"> </span><span class="typ">Thread</span></pre>			<!-- 其他扩展 -->
						
			</div>
			
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link"><a href="https://www.runoob.com/python/python-email.html"><i style="font-size:16px;" class="fa fa-arrow-left" aria-hidden="true"></i></a> <a href="https://www.runoob.com/python/python-email.html" rel="prev" title="Python SMTP发送邮件">Python SMTP发送邮件</a> </div>
			<div class="next-design-link"><a href="https://www.runoob.com/python/python-xml.html" rel="next" title="Python XML 解析">Python XML 解析</a> <a href="https://www.runoob.com/python/python-xml.html"><i style="font-size:16px;" class="fa fa-arrow-right" aria-hidden="true"></i></a></div>
		</div>
		<!-- 笔记列表 -->
		<style>
.wrapper {
  /*text-transform: uppercase; */
  background: #ececec;
  color: #555;
  cursor: help;
  font-family: "Gill Sans", Impact, sans-serif;
  font-size: 20px;
  position: relative;
  text-align: center;
  width: 200px;
  -webkit-transform: translateZ(0); /* webkit flicker fix */
  -webkit-font-smoothing: antialiased; /* webkit text rendering fix */
}

.wrapper .tooltip {
  white-space: nowrap;
  font-size: 14px;
  text-align: left;
  background: #96b97d;
  bottom: 100%;
  color: #fff;
  display: block;
  left: -25px;
  margin-bottom: 15px;
  opacity: 0;
  padding: 14px;
  pointer-events: none;
  position: absolute;
  
  -webkit-transform: translateY(10px);
     -moz-transform: translateY(10px);
      -ms-transform: translateY(10px);
       -o-transform: translateY(10px);
          transform: translateY(10px);
  -webkit-transition: all .25s ease-out;
     -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
       -o-transition: all .25s ease-out;
          transition: all .25s ease-out;
  -webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
     -moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
      -ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
       -o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
          box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
.tooltip a {
	color:#fff;
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
  bottom: -20px;
  content: " ";
  display: block;
  height: 20px;
  left: 0;
  position: absolute;
  width: 100%;
}  

/* CSS Triangles - see Trevor's post */
.wrapper .tooltip:after {
  border-left: solid transparent 10px;
  border-right: solid transparent 10px;
  border-top: solid #96b97d 10px;
  bottom: -10px;
  content: " ";
  height: 0;
  left: 20%;
  margin-left: -13px;
  position: absolute;
  width: 0;
}
.wrapper .tooltip1 {
	margin-left: 50px;
	padding-top: 0px;
}
/*
.wrapper:hover .tooltip {
  opacity: 1;
  pointer-events: auto;
  -webkit-transform: translateY(0px);
     -moz-transform: translateY(0px);
      -ms-transform: translateY(0px);
       -o-transform: translateY(0px);
          transform: translateY(0px);
}
*/
/* IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
  display: none;
}

.lte8 .wrapper:hover .tooltip {
  display: block;
}

</style>

<div class="title" id="comments">
	<h2 class="">
    <div class="altblock">
		    	<i style="font-size:28px;margin-top: 8px;" class="fa fa-pencil-square-o" aria-hidden="true"></i>
    	    </div>
    <span class="mw-headline" id="qa_headline">2  篇笔记</span>
	<span class="mw-headline" id="user_add_note" style="float:right;line-height: 62px;padding-right: 14px;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>  写笔记</span>
    </h2>
</div>

<div id="postcomments">
	<ol class="commentlist">
		<li class="comment even thread-even depth-1" id="comment-37294"><span class="comt-f">#2</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;zhoushixiong</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;men***nyuan@outlook.com</p></div><div id="runoobvote-id-37294" data-commid="37294" class="upvotejs upvotejs-enabled"><a class="upvote"></a> <span class="count">54</span></div></div><div class="comt-main" id="div-comment-37294"><h3 data-title="">Python 线程同步</h3>

<p>以下代码可以直观展示加锁和不加锁时，对数据修改情况。</p>

<p><strong>加锁时</strong></p>

<pre class="prettyprint prettyprinted" style=""><span class="com"># -*-* encoding:UTF-8 -*-</span><span class="pln">
</span><span class="com"># author : shoushixiong</span><span class="pln">
</span><span class="com"># date   : 2018/11/22</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> threading
</span><span class="kwd">import</span><span class="pln"> time
list </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">]</span><span class="pln">
</span><span class="kwd">class</span><span class="pln"> myThread</span><span class="pun">(</span><span class="pln">threading</span><span class="pun">.</span><span class="typ">Thread</span><span class="pun">):</span><span class="pln">
    </span><span class="kwd">def</span><span class="pln"> __init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln">threadId</span><span class="pun">,</span><span class="pln">name</span><span class="pun">,</span><span class="pln">counter</span><span class="pun">):</span><span class="pln">
        threading</span><span class="pun">.</span><span class="typ">Thread</span><span class="pun">.</span><span class="pln">__init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">)</span><span class="pln">
        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">threadId </span><span class="pun">=</span><span class="pln"> threadId
        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">name </span><span class="pun">=</span><span class="pln"> name
        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">counter </span><span class="pun">=</span><span class="pln"> counter
    </span><span class="kwd">def</span><span class="pln"> run</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">):</span><span class="pln">
        </span><span class="kwd">print</span><span class="pln"> </span><span class="str">"开始线程:"</span><span class="pun">,</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">name
        </span><span class="com"># 获得锁，成功获得锁定后返回 True</span><span class="pln">
        </span><span class="com"># 可选的timeout参数不填时将一直阻塞直到获得锁定</span><span class="pln">
        </span><span class="com"># 否则超时后将返回 False</span><span class="pln">
        threadLock</span><span class="pun">.</span><span class="pln">acquire</span><span class="pun">()</span><span class="pln">
        print_time</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">name</span><span class="pun">,</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">counter</span><span class="pun">,</span><span class="pln">list</span><span class="pun">.</span><span class="pln">__len__</span><span class="pun">())</span><span class="pln">
        </span><span class="com"># 释放锁</span><span class="pln">
        threadLock</span><span class="pun">.</span><span class="pln">release</span><span class="pun">()</span><span class="pln">
    </span><span class="kwd">def</span><span class="pln"> __del__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">):</span><span class="pln">
        </span><span class="kwd">print</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">name</span><span class="pun">,</span><span class="str">"线程结束！"</span><span class="pln">
</span><span class="kwd">def</span><span class="pln"> print_time</span><span class="pun">(</span><span class="pln">threadName</span><span class="pun">,</span><span class="pln">delay</span><span class="pun">,</span><span class="pln">counter</span><span class="pun">):</span><span class="pln">
    </span><span class="kwd">while</span><span class="pln"> counter</span><span class="pun">:</span><span class="pln">
        time</span><span class="pun">.</span><span class="pln">sleep</span><span class="pun">(</span><span class="pln">delay</span><span class="pun">)</span><span class="pln">
        list</span><span class="pun">[</span><span class="pln">counter</span><span class="pun">-</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">1</span><span class="pln">
        </span><span class="kwd">print</span><span class="pln"> </span><span class="str">"[%s] %s 修改第 %d 个值，修改后值为:%d"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">ctime</span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">time</span><span class="pun">()),</span><span class="pln">threadName</span><span class="pun">,</span><span class="pln">counter</span><span class="pun">,</span><span class="pln">list</span><span class="pun">[</span><span class="pln">counter</span><span class="pun">-</span><span class="lit">1</span><span class="pun">])</span><span class="pln">
        counter </span><span class="pun">-=</span><span class="pln"> </span><span class="lit">1</span><span class="pln">
threadLock </span><span class="pun">=</span><span class="pln"> threading</span><span class="pun">.</span><span class="typ">Lock</span><span class="pun">()</span><span class="pln">
threads </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[]</span><span class="pln">
</span><span class="com"># 创建新线程</span><span class="pln">
thread1 </span><span class="pun">=</span><span class="pln"> myThread</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="str">"Thread-1"</span><span class="pun">,</span><span class="lit">1</span><span class="pun">)</span><span class="pln">
thread2 </span><span class="pun">=</span><span class="pln"> myThread</span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="str">"Thread-2"</span><span class="pun">,</span><span class="lit">2</span><span class="pun">)</span><span class="pln">
</span><span class="com"># 开启新线程</span><span class="pln">
thread1</span><span class="pun">.</span><span class="pln">start</span><span class="pun">()</span><span class="pln">
thread2</span><span class="pun">.</span><span class="pln">start</span><span class="pun">()</span><span class="pln">
</span><span class="com"># 添加线程到线程列表</span><span class="pln">
threads</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">thread1</span><span class="pun">)</span><span class="pln">
threads</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">thread2</span><span class="pun">)</span><span class="pln">
</span><span class="com"># 等待所有线程完成</span><span class="pln">
</span><span class="kwd">for</span><span class="pln"> t </span><span class="kwd">in</span><span class="pln"> threads</span><span class="pun">:</span><span class="pln">
    t</span><span class="pun">.</span><span class="pln">join</span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="str">"主进程结束！"</span></pre>

<p>输出结果为：</p>

<pre class="prettyprint prettyprinted" style=""><span class="pun">开始线程:</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln">
</span><span class="pun">开始线程:</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">13</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">14</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">15</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">16</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">9</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">17</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">18</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">19</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">20</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">21</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">22</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">23</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">24</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">26</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">28</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">30</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">32</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">9</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">34</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">36</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">38</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">40</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">42</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">44</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">46</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">04</span><span class="pun">:</span><span class="lit">48</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">主进程结束！</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">线程结束！</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">线程结束！</span></pre>

<p><strong>不加锁时</strong></p>同样是上面实例的代码，注释以下两行代码：
<pre class="prettyprint prettyprinted" style=""><span class="pln">threadLock</span><span class="pun">.</span><span class="pln">acquire</span><span class="pun">()</span><span class="pln">
threadLock</span><span class="pun">.</span><span class="pln">release</span><span class="pun">()</span></pre>
<p>输出结果为：</p>






<pre class="prettyprint prettyprinted" style=""><span class="pun">开始线程:</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln">
</span><span class="pun">开始线程:</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">20</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">21</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">21</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">22</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">23</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">9</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">23</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">24</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">26</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">27</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">9</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">27</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">28</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">29</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">29</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">30</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">31</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">31</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">1</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">33</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">35</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">37</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">39</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">41</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">[</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">16</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">43</span><span class="pln"> </span><span class="lit">2018</span><span class="pun">]</span><span class="pln"> </span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">修改第</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">个值，修改后值为:</span><span class="lit">2</span><span class="pln">
</span><span class="pun">主进程结束！</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">线程结束！</span><span class="pln">
</span><span class="typ">Thread</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> </span><span class="pun">线程结束！</span></pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="https://www.runoob.com/note/37294">zhoushixiong</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;zhoushixiong</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;men***nyuan@outlook.com</p></div></span>2年前 (2018-11-22)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-47821"><span class="comt-f">#1</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;qwx</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;807***028@qq.com</p></div><div id="runoobvote-id-47821" data-commid="47821" class="upvotejs upvotejs-enabled"><a class="upvote"></a> <span class="count">19</span></div></div><div class="comt-main" id="div-comment-47821">

<p data-title="Python 线程同步加锁和不加锁">以下代码可以直观展示加锁和不加锁时，线程运行的顺序。</p>

<pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python</span><span class="pln">
</span><span class="com"># -*- coding: UTF-8 -*-</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> threading
</span><span class="kwd">import</span><span class="pln"> time
</span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MyThread</span><span class="pun">(</span><span class="pln">threading </span><span class="pun">.</span><span class="typ">Thread</span><span class="pln"> </span><span class="pun">):</span><span class="pln">
    </span><span class="kwd">def</span><span class="pln"> __init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln">delay</span><span class="pun">,</span><span class="pln">name</span><span class="pun">,</span><span class="pln">count</span><span class="pun">):</span><span class="pln">
        threading </span><span class="pun">.</span><span class="typ">Thread</span><span class="pln"> </span><span class="pun">.</span><span class="pln">__init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">)</span><span class="pln">
        </span><span class="kwd">self</span><span class="pln"> </span><span class="pun">.</span><span class="pln">delay</span><span class="pun">=</span><span class="pln">delay
        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">name</span><span class="pun">=</span><span class="pln">name
        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">count</span><span class="pun">=</span><span class="pln">count
    </span><span class="kwd">def</span><span class="pln"> run </span><span class="pun">(</span><span class="kwd">self</span><span class="pun">):</span><span class="pln">
        </span><span class="kwd">print</span><span class="pun">(</span><span class="str">'starting'</span><span class="pun">+</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">name </span><span class="pun">)</span><span class="pln">
        threadLock</span><span class="pun">.</span><span class="pln">acquire</span><span class="pun">()</span><span class="pln">
        </span><span class="com"># 获得锁，成功获得锁定后返回True</span><span class="pln">
        </span><span class="com"># 可选的timeout参数不填时将一直阻塞直到获得锁定</span><span class="pln">
        </span><span class="com"># 否则超时后将返回False </span><span class="pln">
        print_time</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">delay </span><span class="pun">,</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">name </span><span class="pun">,</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">count</span><span class="pun">)</span><span class="pln">
        </span><span class="com"># 释放锁</span><span class="pln">
        threadLock</span><span class="pun">.</span><span class="pln">release</span><span class="pun">()</span><span class="pln">
</span><span class="kwd">def</span><span class="pln"> print_time</span><span class="pun">(</span><span class="pln">delay</span><span class="pun">,</span><span class="pln">name</span><span class="pun">,</span><span class="pln">count</span><span class="pun">):</span><span class="pln">
    </span><span class="kwd">while</span><span class="pln"> count </span><span class="pun">:</span><span class="pln">
        time</span><span class="pun">.</span><span class="pln">sleep</span><span class="pun">(</span><span class="pln">delay </span><span class="pun">)</span><span class="pln">
        </span><span class="kwd">print</span><span class="pun">(</span><span class="str">"{}:{}"</span><span class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span class="pln">name</span><span class="pun">,</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">ctime</span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">time</span><span class="pun">())))</span><span class="pln">
        count  </span><span class="pun">-=</span><span class="pln"> </span><span class="lit">1</span><span class="pln">
threadLock</span><span class="pun">=</span><span class="pln">threading</span><span class="pun">.</span><span class="typ">Lock</span><span class="pun">()</span><span class="pln">
</span><span class="com">#创建容器（列表）</span><span class="pln">
threads</span><span class="pun">=[]</span><span class="pln">
</span><span class="com"># 创建新线程</span><span class="pln">
thread1</span><span class="pun">=</span><span class="typ">MyThread</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="str">'thread-1'</span><span class="pun">,</span><span class="lit">3</span><span class="pun">)</span><span class="pln">
thread2</span><span class="pun">=</span><span class="typ">MyThread</span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="str">'thread-2'</span><span class="pun">,</span><span class="lit">3</span><span class="pun">)</span><span class="pln">
</span><span class="com"># 开启新线程</span><span class="pln">
thread1</span><span class="pun">.</span><span class="pln">start</span><span class="pun">()</span><span class="pln">
thread2 </span><span class="pun">.</span><span class="pln">start</span><span class="pun">()</span><span class="pln">
</span><span class="com"># 添加线程到线程容器（列表）</span><span class="pln">
threads </span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">thread1 </span><span class="pun">)</span><span class="pln">
threads </span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">thread2 </span><span class="pun">)</span><span class="pln">
</span><span class="com"># 等待所有线程完成</span><span class="pln">
</span><span class="kwd">for</span><span class="pln"> t </span><span class="kwd">in</span><span class="pln"> threads </span><span class="pun">:</span><span class="pln">
    t</span><span class="pun">.</span><span class="pln">join</span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="str">'Exiting Main Thrid'</span><span class="pun">)</span></pre>
<p>加锁时:</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">startingthread</span><span class="pun">-</span><span class="lit">1startingthread</span><span class="pun">-</span><span class="lit">2</span><span class="pln">
thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">44</span><span class="pun">:</span><span class="lit">39</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">44</span><span class="pun">:</span><span class="lit">40</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">44</span><span class="pun">:</span><span class="lit">41</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">44</span><span class="pun">:</span><span class="lit">43</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">44</span><span class="pun">:</span><span class="lit">45</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">44</span><span class="pun">:</span><span class="lit">47</span><span class="pln"> </span><span class="lit">2019Exiting</span><span class="pln"> </span><span class="typ">Main</span><span class="pln"> </span><span class="typ">Thrid</span></pre>
<p>不加锁时：</p>

<pre class="prettyprint prettyprinted" style=""><span class="pln">startingthread</span><span class="pun">-</span><span class="lit">1startingthread</span><span class="pun">-</span><span class="lit">2</span><span class="pln">
thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">48</span><span class="pun">:</span><span class="lit">56</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">48</span><span class="pun">:</span><span class="lit">57</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">48</span><span class="pun">:</span><span class="lit">57</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">1</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">48</span><span class="pun">:</span><span class="lit">58</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">48</span><span class="pun">:</span><span class="lit">59</span><span class="pln"> </span><span class="lit">2019thread</span><span class="pun">-</span><span class="lit">2</span><span class="pun">:</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">49</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> </span><span class="lit">2019</span><span class="pln">
</span><span class="typ">Exiting</span><span class="pln"> </span><span class="typ">Main</span><span class="pln"> </span><span class="typ">Thrid</span></pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="https://www.runoob.com/note/47821">qwx</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;qwx</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;807***028@qq.com</p></div></span>1年前 (2019-10-25)</div></div></li><!-- #comment-## -->
	</ol>
	<div class="pagenav">
			</div>
</div>
<div id="respond" class="no_webshot"> 
		<div class="comment-signarea" style="display:none; padding: 20px 20px;"> 
	<h3 class="text-muted" id="share_code" style="color: #799961;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> 点我分享笔记</h3>
	<!--
	<p style="font-size:14px;">笔记需要是本篇文章的内容扩展！</p><br>
	<p style="font-size:12px;"><a href="//www.runoob.com/tougao" target="_blank">文章投稿，可点击这里</a></p>
	<p style="font-size:14px;"><a href="/w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>
		<h3 class="text-muted"><i class="fa fa-info-circle" aria-hidden="true"></i> 分享笔记前必须<a href="javascript:;" class="runoob-pop">登录</a>！</h3>
		<p><a href="/w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>-->
	</div>
		
	<form action="https://www.runoob.com/wp-content/themes/runoob/option/addnote.php" method="post" id="commentform" style="display:none;"><div id="comment-status" style="display:none;"></div>
		<div class="comt">
			<div class="comt-title">
				<i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i>				<p><a id="cancel-comment-reply-link" href="javascript:;">取消</a></p>
			</div>
			<div class="comt-box">
			<div class="simditor">
  <div class="simditor-wrapper" style="background-color: rgb(238, 238, 238);"><div class="simditor-toolbar" style="top: 0px; background-color: rgb(238, 238, 238);"><ul><li><a tabindex="-1" unselectable="on" class="toolbar-item toolbar-item-bold" href="javascript:;" title="加粗文字 ( Ctrl + b )"><span class="simditor-icon simditor-icon-bold"></span></a></li><li><a tabindex="-1" unselectable="on" class="toolbar-item toolbar-item-code" href="javascript:;" title="插入代码"><span class="simditor-icon simditor-icon-code"></span></a></li><li><a tabindex="-1" unselectable="on" class="toolbar-item toolbar-item-ul" href="javascript:;" title="无序列表 ( Ctrl + . )"><span class="simditor-icon simditor-icon-list-ul"></span></a></li><li><a tabindex="-1" unselectable="on" class="toolbar-item toolbar-item-ol" href="javascript:;" title="有序列表 ( ctrl + / )"><span class="simditor-icon simditor-icon-list-ol"></span></a></li><li><a tabindex="-1" unselectable="on" class="toolbar-item toolbar-item-image" href="javascript:;" title="插入图片"><span class="simditor-icon simditor-icon-picture-o"></span></a></li></ul></div>
    <div class="simditor-placeholder" style="display: block;">写笔记...</div>
    <div class="simditor-body" contenteditable="true"><p><br></p></div>
  <div id="mded"></div></div>
<div class="simditor-popover code-popover"><div class="code-settings">
  <div class="settings-field">
    <select class="select-lang">
      <option value="-1">选择程序语言</option>
    <option value="bash">Bash</option><option value="c++">C++</option><option value="cs">C#</option><option value="css">CSS</option><option value="erlang">Erlang</option><option value="less">Less</option><option value="sass">Sass</option><option value="diff">Diff</option><option value="coffeescript">CoffeeScript</option><option value="html">HTML,XML</option><option value="json">JSON</option><option value="java">Java</option><option value="js">JavaScript</option><option value="markdown">Markdown</option><option value="oc">Objective C</option><option value="php">PHP</option><option value="parl">Perl</option><option value="python">Python</option><option value="ruby">Ruby</option><option value="sql">SQL</option></select>
  </div>
</div></div><div class="simditor-popover image-popover"><div class="link-settings">
  <div class="settings-field">
    <label>图片地址</label>
    <input class="image-src" type="text" tabindex="1">
    
  </div>
  <div class="settings-field">
    <label>图片描述</label>
    <input class="image-alt" id="image-alt" type="text" tabindex="1">
  </div>
  <div class="settings-field">
    <label>图片尺寸</label>
    <input class="image-size" id="image-width" type="text" tabindex="2">
    <span class="times">×</span>
    <input class="image-size" id="image-height" type="text" tabindex="3">
    <a class="btn-restore" href="javascript:;" title="还原图片尺寸" tabindex="-1">
      <span class="simditor-icon simditor-icon-undo"></span>
    </a>
  </div>
</div></div></div>
			
				<div class="comt-ctrl" style="background-color: rgb(238, 238, 238);">
					<div class="comt-tips"><input type="hidden" name="comment_post_ID" value="4616" id="comment_post_ID">
<input type="hidden" name="comment_parent" id="comment_parent" value="0">
</div>
					<button type="submit" name="submit" id="submit" tabindex="5"><i class="fa fa-pencil" aria-hidden="true"></i> 分享笔记</button>
				</div>
			</div>
		
				
					<div class="comt-comterinfo"> 
						<ul id="comment-author-info">
							<li class="form-inline"><label class="hide" for="author">昵称</label><input class="ipt" type="text" name="author" id="author" value="" tabindex="2" placeholder="昵称" style="background-color: rgb(238, 238, 238);"><span class="text-muted">昵称 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="email">邮箱</label><input class="ipt" type="text" name="email" id="email" value="" tabindex="3" placeholder="邮箱" style="background-color: rgb(238, 238, 238);"><span class="text-muted">邮箱 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="url">引用地址</label><input class="ipt" type="text" name="url" id="url" value="" tabindex="4" placeholder="引用地址" style="background-color: rgb(238, 238, 238);"><span class="text-muted">引用地址</span></li>
						</ul>
					</div>
				
			
		</div>

	</form>
	</div>
<script type="text/javascript">
$(function() {
	//初始化编辑器
	
	var editor = new Simditor({
	  textarea: $('#mded'),
	  placeholder: '写笔记...',
	  upload:false,
	 // upload: {url:'/api/comment_upload_file.php',params: null,fileKey: 'upload_file',connectionCount: 1,leaveConfirm: '文件正在上传，您确定离开?'},
	  defaultImage: 'http://www.runoob.com/images/logo.png',
	  codeLanguages: '',
	  autosave: 'editor-content',
	  toolbar: [  'bold','code','ul','ol','image' ]
	});
	editor.on('selectionchanged', function() {
		$(".code-popover").hide();
	});

	// 提交数据
	$("#share_code").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		
	});
	$("#user_add_note").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		$('html, body').animate({
       	    scrollTop: $("#respond").offset().top
    	}, 200);
	});

	// 提交笔记
	var commentform=$('#commentform');
	commentform.prepend('<div id="comment-status" style="display:none;" ></div>');
	var statusdiv=$('#comment-status');
	
	commentform.submit(function(e){
		e.preventDefault();
		var noteContent = editor.getValue();
		// console.log(noteContent);
		noteContent = noteContent.replace(/<pre><code>/g,"<pre>");
		noteContent = noteContent.replace(/<\/code><\/pre>/g,"</pre>");
		
		// 系列化表单数据
		var comment_parent = 0;
		var is_user_logged_in = $("#is_user_logged_in").val();
		var comment_post_ID =  4616;
		var _wp_unfiltered_html_comment = $("#_wp_unfiltered_html_comment").val();
		var comment = noteContent;
		var author = $("#author").val();
		var url = $("#url").val();
		var email = $("#email").val();
		if(isBlank(author) && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入昵称！</p>').show();
		} else if(isBlank(email)  && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入邮箱！</p>').show();
		} else {
			// var formdata=commentform.serialize() + "&comment=" + noteContent ;
			// 添加状态信息
			statusdiv.html('<p>Processing...</p>').show();
			// 获取表单提交地址
			var formurl=commentform.attr('action');
			
			// 异步提交
			$.ajax({
					type: 'post',
					url: formurl,
					dataType:'json',
					data: {"comment_parent":comment_parent,"comment_post_ID":comment_post_ID, "_wp_unfiltered_html_comment":_wp_unfiltered_html_comment,"comment":comment,"url":url, "email":email,"author":author},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					statusdiv.html('<p class="ajax-error" >数据不完整或表单提交太快了！</p>').show();
				},
				success: function(data, textStatus){
					if(data.errorno=="0") {
						$("#submit").prop('disabled', true);
						statusdiv.html('<p class="ajax-success" >笔记已提交审核，感谢分享笔记！</p>').show();
						alert('笔记已提交审核，感谢分享笔记！');
					}else{
						statusdiv.html('<p class="ajax-error" >'+data.msg+'</p>').show();
					}
					commentform.find('textarea[name=comment]').val('');
				}
			});
			setTimeout(function(){
		        $("#submit").prop('disabled', false);
		    }, 10*1000);
		}
		return false;

	});
	$(".comt-author").click(function() {
		href = $(this).children("a").attr("href");
		if(href.indexOf("/note/")!=-1) {
			var win = window.open(href, '_blank');
  			win.focus();
		}
	});
	$(".comt-meta span").hover(function(){
		$(this).children(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).children(".tooltip").removeAttr("style");
	});
	/*
	$(".wrapper i").hover(function(){
		$(this).siblings(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).siblings(".tooltip").css({ "opacity": 0, "pointer-events": "auto"});
	});
	*/
	//Upvote.create('runoobvote-id', {callback: vote_callback});
	var ajaxurl = 'https://www.runoob.com/wp-admin/admin-ajax.php';
	var callback = function(data) {
		//console.log($('#runoobvote-id').upvote('upvoted'));
		//console.log($('#runoobvote-id').upvote('downvoted'));
		//console.log(data);
		_vote_action = data.action;
		id_arr = data.id.split('-');
		um_id= id_arr[2];
		//console.log(um_id);
		
		var re = /^[1-9]+/;
		if (re.test(um_id)) { 
			var ajax_data = {
				_vote_action: _vote_action,
				action: "pinglun_zan",
				um_id: um_id,
				um_action: "ding"
			};
			//console.log(ajax_data);
			$.post(ajaxurl,ajax_data,function(status){
				if(status.vote_num>999) {
					_voteHtml = '<span style="display: block; text-align: center;font-size: 20px; color: #6a737c; margin: 8px 0;">'+kFormatter(status.vote_num) +'</span>';
					$("#runoobvote-id-" + um_id + " .count").hide().after(_voteHtml);
				}
				
			});
		}
	};
	if($('#comments').length){
		$('.upvotejs').upvote({id: 4616, callback: callback});
	
	$.post(ajaxurl,{"action":"pinglun_zan","postid":4616},function(data){  
			$(data).each(function(key,value) {
				$("#runoobvote-id-" + value.commid + " .upvote").addClass(value.upvotejs_class);
				$("#runoobvote-id-" + value.commid + " .downvote").addClass(value.downvote_class);
				if(value.upvote_count>999) {
					_voteHtml = '<span style="display: block; text-align: center;font-size: 20px; color: #6a737c; margin: 8px 0;">'+kFormatter(value.upvote_count) +'</span>';
					$("#runoobvote-id-" + value.commid + " .count").hide().after(_voteHtml);
				} else {
					$("#runoobvote-id-" + value.commid + " .count").text(value.upvote_count);
				}
				
				
				
			})
		},'json');
		
	}
	
	
});
function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}
function kFormatter(num) {
	// return num;
    return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}

</script>

<link rel="stylesheet" href="./Python 多线程 _ 菜鸟教程_files/upvotejs.css">
<script src="./Python 多线程 _ 菜鸟教程_files/upvotejs.vanilla.js"></script>
<script src="./Python 多线程 _ 菜鸟教程_files/upvotejs.jquery.js"></script>
<link rel="stylesheet" href="./Python 多线程 _ 菜鸟教程_files/qa.css">
<link rel="stylesheet" type="text/css" href="./Python 多线程 _ 菜鸟教程_files/simditor.css">
<script type="text/javascript" src="./Python 多线程 _ 菜鸟教程_files/module.js"></script>
<script type="text/javascript" src="./Python 多线程 _ 菜鸟教程_files/hotkeys.js"></script>
<script type="text/javascript" src="./Python 多线程 _ 菜鸟教程_files/uploader.js"></script>
<script type="text/javascript" src="./Python 多线程 _ 菜鸟教程_files/simditor.min.js"></script>
<script type="text/javascript" src="./Python 多线程 _ 菜鸟教程_files/simditor-autosave.js"></script>
		<div class="sidebar-box ">
				<div id="ad-336280">

		<style>	
.responsive_ad1 { display:none; }
@media(min-width: 800px) { .responsive_ad1 {  display:block;margin:0 auto;} }

</style>
<script async="" src="./Python 多线程 _ 菜鸟教程_files/f(7).txt"></script>
<!-- 移动版 自动调整 -->
<ins class="adsbygoogle responsive_ad1" style="min-width: 400px; max-width: 728px; width: 728px; height: 90px;" data-ad-client="ca-pub-5751451760833794" data-ad-slot="1691338467" data-full-width-responsive="true" data-adsbygoogle-status="done"><ins id="aswift_0_expand" style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent;" tabindex="0" title="Advertisement" aria-label="Advertisement"><ins id="aswift_0_anchor" style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent;"><iframe id="aswift_0" name="aswift_0" style="left:0;position:absolute;top:0;border:0;width:728px;height:90px;" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="728" height="90" frameborder="0" src="./Python 多线程 _ 菜鸟教程_files/ads.html" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!1" data-google-query-id="CMyboM_7l-4CFY8PvQodzcoLZA" data-load-complete="true"></iframe></ins></ins></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
		</div>
				</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
<!--
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		 <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>
		</div>
		
	</div>
	-->
<style>
.sidebar-tree .double-li {
	width:300px;
}
.sidebar-tree .double-li li {
    width: 44%;
    line-height: 1.5em;
    border-bottom: 1px solid #ccc;
    float: left;
    display: inline;
}
</style>

	
		<div class="sidebar-box re-box re-box-large">
		<div class="sidebar-box recommend-here" style="margin: 0 auto;">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;"> <i class="fa fa-list" aria-hidden="true"></i> 分类导航</a>
		</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree">
			<ul><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> HTML / CSS</a><ul class="double-li"><li><a title="HTML 教程" href="https://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="https://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="https://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="https://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap3 教程" href="https://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap3 教程</a></li><li><a title="Bootstrap4 教程" href="https://www.runoob.com/bootstrap4/bootstrap4-tutorial.html">Bootstrap4 教程</a></li><li><a title="Font Awesome 教程" href="https://www.runoob.com/font-awesome/fontawesome-tutorial.html">Font Awesome 教程</a></li><li><a title="Foundation 教程" href="https://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> JavaScript</a><ul class="double-li"><li><a title="JavaScript 教程" href="https://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="https://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="https://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="https://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="AngularJS2 教程" href="https://www.runoob.com/angularjs2/angularjs2-tutorial.html">AngularJS2 教程</a></li><li><a title="Vue.js 教程" href="https://www.runoob.com/vue2/vue-tutorial.html">Vue.js 教程</a></li><li><a title="React 教程" href="https://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="TypeScript 教程" href="https://www.runoob.com/typescript/ts-tutorial.html">TypeScript 教程</a></li><li><a title="jQuery UI 教程" href="https://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="https://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="https://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="https://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="https://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Echarts 教程" href="https://www.runoob.com/echarts/echarts-tutorial.html">Echarts 教程</a></li><li><a title="Highcharts 教程" href="https://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="https://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 服务端</a><ul class="double-li"><li><a title="Python 教程" href="https://www.runoob.com/python3/python3-tutorial.html">Python 教程</a></li><li><a title="Python2.x 教程" href="https://www.runoob.com/python/python-tutorial.html">Python2.x 教程</a></li><li><a title="Linux 教程" href="https://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="https://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="https://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="https://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="https://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="https://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="https://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="https://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="https://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="https://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Rust 教程" href="https://www.runoob.com/rust/rust-tutorial.html">Rust 教程</a></li><li><a title="R 教程" href="https://www.runoob.com/r/r-tutorial.html">R 教程</a></li><li><a title="Scala 教程" href="https://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="https://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="PHP 教程" href="https://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Django 教程" href="https://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Zookeeper 教程" href="https://www.runoob.com/w3cnote/zookeeper-tutorial.html">Zookeeper 教程</a></li><li><a title="设计模式" href="https://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="https://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="Maven 教程" href="https://www.runoob.com/maven/maven-tutorial.html">Maven 教程</a></li><li><a title="NumPy 教程" href="https://www.runoob.com/numpy/numpy-tutorial.html">NumPy 教程</a></li><li><a title="Verilog 教程" href="https://www.runoob.com/w3cnote/verilog-tutorial.html">Verilog 教程</a></li><li><a title="ASP 教程" href="https://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="https://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="https://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 数据库</a><ul class="double-li"><li><a title="SQL 教程" href="https://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="https://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="PostgreSQL 教程" href="https://www.runoob.com/postgresql/postgresql-tutorial.html">PostgreSQL 教程</a></li><li><a title="SQLite 教程" href="https://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="https://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="https://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="https://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 移动端</a><ul class="double-li"><li><a title="Android 教程" href="https://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="https://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="https://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="https://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li><li><a title="Kotlin 教程" href="https://www.runoob.com/kotlin/kotlin-tutorial.html">Kotlin 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> XML 教程</a><ul class="double-li"><li><a title="XML 教程" href="https://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="https://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="https://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="https://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="https://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="https://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="https://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="https://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="https://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="https://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="https://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> ASP.NET</a><ul class="double-li"><li><a title="ASP.NET 教程" href="https://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="https://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="https://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="https://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="https://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="https://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> Web Service</a><ul class="double-li"><li><a title="Web Service 教程" href="https://www.runoob.com/webservices/webservices-tutorial.html">Web Service 教程</a></li><li><a title="WSDL 教程" href="https://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="https://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="https://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="https://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 开发工具</a><ul class="double-li"><li><a title="Eclipse 教程" href="https://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="https://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Svn 教程" href="https://www.runoob.com/svn/svn-tutorial.html">Svn 教程</a></li><li><a title="Markdown 教程" href="https://www.runoob.com/markdown/md-tutorial.html">Markdown 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 网站建设</a><ul class="double-li"><li><a title="HTTP 教程" href="https://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="https://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="https://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="https://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="https://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="https://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="https://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	</div>
	<br>
	
	<div class="sidebar-box re-box re-box-large">
		<div class="sidebar-box recommend-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="re-600160" id="sidebar-right-re">
		<a data-tt="2392237" onclick="_hmt.push([&#39;_trackEvent&#39;, &#39;re-da&#39;, &#39;click&#39;, &#39;c-48&#39;])" href="https://datayi.cn/w/qPkMNZk9" target="_blank"><img width="160px" src="./Python 多线程 _ 菜鸟教程_files/Python3.png"><p></p></a>		</div>
	</div>
</div></div>

</div>

<script>
var aid = 4616;
function coll() {
	$.post( '/wp-content/themes/runoob/option/user/userinfo.php', {aid:aid, action:"collarticle", opt:'add'},function( data ) {
		if(data.error==0) {
			$("#content").find("h1:first").find("a").attr("href","javascript:void(0);");
			$("#content").find("h1:first").find("img").attr("src","http://www.runoob.com/wp-content/themes/runoob/assets/img/coll2.png").css({width:32+"px",height:32+"px"});
		}
		alert(data.msg);
	},'json');
}
</script>


<!-- 反馈对话框开始 -->
<script src="./Python 多线程 _ 菜鸟教程_files/feedback.js"></script>
<link rel="stylesheet" href="./Python 多线程 _ 菜鸟教程_files/feedback.css">
<script type="text/javascript">
$.feedback({
    ajaxURL: '/feedback/runoob_feedback.php',
	html2canvasURL: '/wp-content/themes/runoob/assets/feedback/stable/2.0/html2canvas.js',
	onClose: function () {
         window.location.reload();
    }
});
</script><button class="feedback-btn feedback-btn-gray" style="display: inline-block;">反馈/建议</button>
<!-- 反馈对话框结束 -->
<button class="feedback-btn feedback-btn-gray" style="display: inline-block;">反馈/建议</button>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="runoob-block">
    <div class="runoob cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       ·<a target="_blank" href="https://www.runoob.com/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       ·<a target="_blank" href="https://www.runoob.com/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       ·<a target="_blank" href="https://www.runoob.com/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       ·<a target="_blank" href="https://www.runoob.com/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       ·<a target="_blank" href="https://www.runoob.com/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       ·<a target="_blank" href="https://www.runoob.com/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       ·<a target="_blank" href="https://www.runoob.com/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&amp;工具
      </dt>
      <dd>
       · <a target="_blank" href="https://www.runoob.com/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       · <a target="_blank" href="https://www.runoob.com/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       · <a target="_blank" href="https://www.runoob.com/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       · <a target="_blank" href="https://www.runoob.com/tags/html-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       · <a target="_blank" href="https://www.runoob.com/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       · <a target="_blank" href="https://c.runoob.com/front-end/53">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       ·
      <a href="http://www.runoob.com/js/js-chrome.html" title="Chrome 浏览器中执行 JavaScript">Chrome 浏览器中...</a>
      </dd>
              <dd>
       ·
      <a href="http://www.runoob.com/w3cnote/verilog-dds.html" title="7.6 Verilog DDS 设计">7.6 Verilog DDS...</a>
      </dd>
              <dd>
       ·
      <a href="http://www.runoob.com/w3cnote/verilog-numerical-conversion.html" title="8.1 Verilog 数值转换">8.1 Verilog 数...</a>
      </dd>
              <dd>
       ·
      <a href="http://www.runoob.com/w3cnote/verilog-fft.html" title="7.5 Verilog FFT 设计">7.5 Verilog FFT...</a>
      </dd>
              <dd>
       ·
      <a href="http://www.runoob.com/w3cnote/verilog-cic.html" title="7.4 Verilog CIC 滤波器设计">7.4 Verilog CIC...</a>
      </dd>
              <dd>
       ·
      <a href="http://www.runoob.com/w3cnote/verilog-serial-fir.html" title="7.3 Verilog 串行 FIR 滤波器设计">7.3 Verilog 串...</a>
      </dd>
              <dd>
       ·
      <a href="http://www.runoob.com/w3cnote/verilog-fir.html" title="7.2 Verilog 并行 FIR 滤波器设计">7.2 Verilog 并...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       ·
       <a target="_blank" href="https://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
        <!--
      <dd>
       &middot;
      <a class="wxpopup" onclick="popFunction()">加入我们
       <span class="popuptext" id="myPopup">
       岗位：技术类型自媒体<br>
工作地点：厦门<br>

具体联系邮箱：<strong>429240967@qq.com</strong></span>
      </a>
      </dd> 
      -->
      <dd>
       ·
      <a target="_blank" href="https://www.runoob.com/disclaimer">免责声明</a>
       </dd>
       <!--
       <dd style="display: block;">
       &middot;
      <a href="javascript:void(0)" onclick="dashangToggle()" style="font-weight:bold;color:#f00;" title="打赏，支持一下">打赏一下</a>
       </dd>
     -->
      <dd>
       ·
       <a target="_blank" href="https://www.runoob.com/aboutus">关于我们</a>
       </dd>
      <dd>
       ·
      <a target="_blank" href="https://www.runoob.com/archives">文章归档</a>
      </dd>
    
     </dl>
    
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
            <img width="128" height="128" src="./Python 多线程 _ 菜鸟教程_files/qrcode.png">
       </div>
     </div>
     
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright © 2013-2021    <strong><a href="https://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="https://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/">闽ICP备15012807号-1</a>
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部" style="display: inline-block;"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode" href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" style="" href="javascript:void(0)" title="标记/收藏"><i class="fa fa-star" aria-hidden="true"></i></a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="./Python 多线程 _ 菜鸟教程_files/qrcode.png"></div> 
    </div>
  </div>

  <div class="hide_box"></div>
    <div class="shang_box">
      <a class="shang_close" href="javascript:void(0)" onclick="dashangToggle()" title="关闭"><img src="./Python 多线程 _ 菜鸟教程_files/close.jpg" alt="取消"></a>
       
      <div class="shang_tit">
        <p>感谢您的支持，我会继续努力的!</p>
      </div>
      <div class="shang_payimg">
        <img src="./Python 多线程 _ 菜鸟教程_files/weipayimg.png" alt="扫码支持" title="扫一扫">
      </div>
        <div class="pay_explain">扫码打赏，你说多少就多少</div>
      <div class="shang_payselect">
        <div class="pay_item  checked" data-id="weipay">
          <span class="radiobox"></span>
          <span class="pay_logo"><img src="./Python 多线程 _ 菜鸟教程_files/wechat.jpg" alt="微信"></span>
        </div>
        <div class="pay_item" data-id="alipay">
          <span class="radiobox"></span>
          <span class="pay_logo"><img src="./Python 多线程 _ 菜鸟教程_files/alipay.jpg" alt="支付宝"></span>
        </div>
        
      </div>
      <div class="shang_info">
        <p>打开<span id="shang_pay_txt">支付宝</span>扫一扫，即可进行扫码打赏哦</p>
        <p><a href="https://c.runoob.com/codedemo/5348" target="_blank"><span style=" font-size: 14px;color: #000;font-weight: bold;">点我查看本站打赏源码！</span></a></p>
      </div>
    </div>
  <div id="testClick"></div>
 <div style="display:none;">
 
 <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3eec0b7da6548cf07db3bc477ea905ee";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
<script async="" src="./Python 多线程 _ 菜鸟教程_files/js"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-84264393-2');
</script>
</div>
<script>
window.jsui={
    www: 'https://www.runoob.com',
    uri: 'https://www.runoob.com/wp-content/themes/runoob'
};
</script>
<style>
ol,ul{list-style:none}.cd-switcher a{text-decoration:none;outline:0}.cd-switcher a:hover{text-decoration:underline}a:focus{outline:0;-moz-outline:0}.main_nav{width:300px;height:60px;margin:60px auto 10px auto}.main_nav li{float:left;width:60px;margin-right:10px;font-size:16px;padding:.6em 1em;border-radius:3em;background:#2f889a;text-align:center}.main_nav li a{color:#fff}.errtip{background-color:#fceaea;color:#db5353;padding:8px 15px;font-size:14px;border:1px solid #fc9797;border-radius:5px}.cd-user-modal{position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(52,54,66,0.9);z-index:3;overflow-y:auto;cursor:pointer;visibility:hidden;opacity:0;-webkit-transition:opacity .3s 0,visibility 0 .3s;-moz-transition:opacity .3s 0,visibility 0 .3s;transition:opacity .3s 0,visibility 0 .3s}.cd-user-modal.is-visible{visibility:visible;opacity:1;-webkit-transition:opacity .3s 0,visibility 0 0;-moz-transition:opacity .3s 0,visibility 0 0;transition:opacity .3s 0,visibility 0 0}.cd-user-modal.is-visible .cd-user-modal-container{-webkit-transform:translateY(0);-moz-transform:translateY(0);-ms-transform:translateY(0);-o-transform:translateY(0);transform:translateY(0)}.cd-user-modal-container{position:relative;width:90%;max-width:500px;background:#FFF;margin:3em auto 4em;cursor:auto;border-radius:.25em;-webkit-transform:translateY(-30px);-moz-transform:translateY(-30px);-ms-transform:translateY(-30px);-o-transform:translateY(-30px);transform:translateY(-30px);-webkit-transition-property:-webkit-transform;-moz-transition-property:-moz-transform;transition-property:transform;-webkit-transition-duration:.3s;-moz-transition-duration:.3s;transition-duration:.3s}.cd-user-modal-container .cd-switcher:after{content:"";display:table;clear:both}.cd-user-modal-container .cd-switcher li{width:50%;float:left;text-align:center}.cd-user-modal-container .cd-switcher li:first-child a{border-radius:.25em 0 0 0}.cd-user-modal-container .cd-switcher li:last-child a{border-radius:0 .25em 0 0}.cd-user-modal-container .cd-switcher a{font-size:1.2em;font-weight:bold;display:block;width:100%;height:50px;line-height:50px;background:#e8f1e2;color:#96b880}.cd-user-modal-container .cd-switcher a.selected{background:#FFF;color:#505260}@media only screen and (min-width:600px){.cd-user-modal-container{margin:4em auto}.cd-user-modal-container .cd-switcher a{height:70px;line-height:70px}}.cd-form{padding:1.4em}.cd-form .fieldset{position:relative;margin:1.4em 0}.cd-form .fieldset:first-child{margin-top:0}.cd-form .fieldset:last-child{margin-bottom:0}.cd-form label{font-size:16px;font-size:.875rem}.cd-form label.image-replace{display:inline-block;position:absolute;left:15px;top:50%;bottom:auto;-webkit-transform:translateY(-50%);-moz-transform:translateY(-50%);-ms-transform:translateY(-50%);-o-transform:translateY(-50%);transform:translateY(-50%);height:20px;width:20px;overflow:hidden;text-indent:100%;white-space:nowrap;color:transparent;text-shadow:none;background-repeat:no-repeat;background-position:50% 0}.cd-form label.cd-username{background-image:url("/wp-content/themes/runoob/assets/img/cd-icon-username.svg")}.cd-form label.cd-email{background-image:url("/wp-content/themes/runoob/assets/img/cd-icon-email.svg")}.cd-form label.cd-password{background-image:url("/wp-content/themes/runoob/assets/img/cd-icon-password.svg")}.cd-form input{margin:0;padding:0;border-radius:.25em}.cd-form input.full-width{width:80%}.cd-form input.full-width2{width:94%}.cd-form input.has-padding{padding:12px 20px 12px 50px}.cd-form input.has-border{border:1px solid #d2d8d8;-webkit-appearance:none;-moz-appearance:none;-ms-appearance:none;-o-appearance:none;appearance:none}.cd-form input.has-border:focus{border-color:#98b880;box-shadow:0 0 5px rgba(52,54,66,0.1);outline:0}.cd-form input.has-error{border:1px solid #d76666}.cd-form input[type=password]{padding-right:65px}.cd-form input[type=submit]{padding:16px 0;cursor:pointer;background:#96b97d;color:#FFF;font-weight:bold;border:0;-webkit-appearance:none;-moz-appearance:none;-ms-appearance:none;-o-appearance:none;appearance:none;font-size:1.2em;font-weight:bold}.no-touch .cd-form input[type=submit]:hover,.no-touch .cd-form input[type=submit]:focus{background:#3599ae;outline:0}@media only screen and (min-width:600px){.cd-form{padding:2em}.cd-form .fieldset{margin:2em 0}.cd-form .fieldset:first-child{margin-top:0}.cd-form .fieldset:last-child{margin-bottom:0}.cd-form input.has-padding{padding:16px 20px 16px 50px}.cd-form input[type=submit]{padding:16px 0}}.cd-close-form{display:block;position:absolute;width:40px;height:40px;right:0;top:-40px;background:url("/wp-content/themes/runoob/assets/img/cd-icon-close.svg") no-repeat center center;text-indent:100%;white-space:nowrap;overflow:hidden}@media only screen and (min-width:1170px){}#cd-login,#cd-signup,#cd-reset-password{display:none}#cd-login.is-selected,#cd-signup.is-selected,#cd-reset-password.is-selected{display:block}
</style>
<div class="cd-user-modal"> 
	<div class="cd-user-modal-container">
		<ul class="cd-switcher">
			<li><a href="javascript:;">用户登录</a></li>
			<li><a href="javascript:;">注册新用户</a></li>
		</ul>

		<div id="cd-login"> <!-- 登录表单 -->
			<div class="cd-form">
				<p class="fieldset">
					<label class="image-replace cd-username" for="signin-username">用户名</label>
					<input class="full-width has-padding has-border" id="signin-username" name="username" type="text" placeholder="输入用户名">
				</p>

				<p class="fieldset">
					<label class="image-replace cd-password" for="signin-password">密码</label>
					<input class="full-width has-padding has-border" id="signin-password" name="password" type="password" placeholder="输入密码">
				</p>
				
				<p class="fieldset">
					<input type="checkbox" id="remember-me" checked="">
					<label for="remember-me">记住登录状态</label>
          <a href="https://www.runoob.com/reset-password" style="float: right;padding-right: 20px;" target="_blank">忘记密码？</a>
				</p>
 				<input type="hidden" name="action" value="signin">
				<p class="fieldset">
					<input class="full-width2" type="submit" value="登 录">
				</p>
        <div class="err-msg"></div>
			</div>
		</div>

		<div id="cd-signup"> <!-- 注册表单 -->
			<div class="cd-form">
				<p class="fieldset">
					<label class="image-replace cd-password" for="verifycode">邀请码</label>
					<input class="full-width has-padding has-border" id="signup-verifycode" name="verifycode" type="text" placeholder="输入邀请码">
				</p>
				<p class="fieldset">
					<label class="image-replace cd-username" for="signup-username">用户名</label>
					<input class="full-width has-padding has-border" id="signup-username" name="name" type="text" placeholder="输入用户名">
				</p>

				<p class="fieldset">
					<label class="image-replace cd-email" for="signup-email">邮箱</label>
					<input class="full-width has-padding has-border" name="email" id="signup-email" type="email" placeholder="输入mail">
				</p>

				<p class="fieldset">
					<label class="image-replace cd-password" for="signup-password">密码</label>
					<input class="full-width has-padding has-border" id="signup-password" name="password" type="password" placeholder="输入密码">
				</p>
				<p class="fieldset">
					<label class="image-replace cd-password" for="signup-password2">重复输入密码</label>
					<input class="full-width has-padding has-border" id="signup-password2" name="password2" type="password" placeholder="重复输入密码">
				</p>
				
				<!-- 
				<p class="fieldset">
					<input type="checkbox" id="accept-terms">
					<label for="accept-terms">我已阅读并同意 <a href="javascript:;">用户协议</a></label>
				</p>
				 -->
				 
				 <input type="hidden" name="action" value="signup">
				<p class="fieldset">
					<input class="full-width2" type="submit" value="注册新用户">
				</p>
				<p class="fieldset">
				  <a href="https://www.runoob.com/w3cnote/runoob-user-test-intro.html#invite" target="_blank">如何获取邀请码？</a>
				  </p>
				<div class="err-msg"></div>
			</div>

		</div>

		<a href="javascript:;" class="cd-close-form">关闭</a>
	</div>
</div> 
<script src="./Python 多线程 _ 菜鸟教程_files/main.min.js"></script>


<ins class="adsbygoogle adsbygoogle-noablate" data-adsbygoogle-status="done" style="display: none !important;"><ins id="aswift_1_expand" style="display:inline-table;border:none;height:0px;margin:0;padding:0;position:relative;visibility:visible;width:0px;background-color:transparent;" tabindex="0" title="Advertisement" aria-label="Advertisement"><ins id="aswift_1_anchor" style="display:block;border:none;height:0px;margin:0;padding:0;position:relative;visibility:visible;width:0px;background-color:transparent;"><iframe id="aswift_1" name="aswift_1" style="left:0;position:absolute;top:0;border:0;width:undefinedpx;height:undefinedpx;" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" frameborder="0" src="./Python 多线程 _ 菜鸟教程_files/ads(1).html" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!1" data-load-complete="true"></iframe></ins></ins></ins><iframe id="google_osd_static_frame_7250662531165" name="google_osd_static_frame" style="display: none; width: 0px; height: 0px;" src="./Python 多线程 _ 菜鸟教程_files/saved_resource.html"></iframe></body><iframe id="google_esf" name="google_esf" src="./Python 多线程 _ 菜鸟教程_files/zrt_lookup.html" data-ad-client="ca-pub-5751451760833794" style="display: none;"></iframe></html>